﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;
using Telecom.Service.EntityCustom;

namespace Telecom.Service
{
    public class cauhinhTBSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public cauhinhTBSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal addorUpdate(CAUHINH_TBI entity)
        {
            if (entity.ID == 0) dbContext.CAUHINH_TBI.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.ID;
        }

        public ListObjectToJtable<CAUHINH_TBI> getList(int? LapTbId, int jtStartIndex = 0, int jtPageSize = 0)
        {
            ListObjectToJtable<CAUHINH_TBI> _listReturnOjnect = new ListObjectToJtable<CAUHINH_TBI>();

            try
            {
                var _listReturnALl = dbContext.CAUHINH_TBI.Where(p => (p.THIETBIID == LapTbId || LapTbId == 0)).ToList();
                
               _listReturnOjnect.count = _listReturnALl.Count();

                if (jtPageSize != 0)
                {
                    _listReturnALl = _listReturnALl.Skip(jtStartIndex).Take(jtPageSize).ToList();
                }

                _listReturnOjnect.list = _listReturnALl;

                return _listReturnOjnect;
                
            }
            catch (Exception e)
            {
                ListObjectToJtable<CAUHINH_TBI> _listRe = new ListObjectToJtable<CAUHINH_TBI>();
                return _listRe;
            }
        }

        public CAUHINH_TBI GetById(int Id)
        {
            try
            {
                return dbContext.CAUHINH_TBI.Where(p => p.ID == Id).First();
            }
            catch (Exception ex)
            {
                CAUHINH_TBI _ml = new CAUHINH_TBI();
                return _ml;
            }
        }

        public List<CAUHINH_TBI> GetListByLapTbbiet(int lapTBId)
        {
            try
            {
                return dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == lapTBId).ToList();
            }
            catch (Exception ex)
            {
                List<CAUHINH_TBI> _ml = new List<CAUHINH_TBI>();
                return _ml;
            }
        }

        public decimal getFlotMax(int lapTBId)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == lapTBId).OrderByDescending(p => p.ID).First().SLOT.Value;
            }
            catch (Exception e)
            {
                return 0;
            }
        }


        public bool Detele(int Id)
        {
            try
            {
                var _entity = GetById(Id);
                dbContext.CAUHINH_TBI.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex) { return false; }
        }
    }
}
